from flask import Flask
from .settings import config
from .extensions import bootstrap, db, migrate, login_manager
from .models import Admin, Questionnaire
from .main import main_bp
from .admin import admin_bp
from .commands import register_commands

def create_app(config_type='development'):
    app = Flask('app')
    register_config(app, config_type)
    register_extensions(app)
    register_blueprints(app)
    register_commands(app)
    register_context(app)
    return app


def register_config(app: Flask, config_name: str):
    app.config.from_object(config[config_name])
    app.jinja_env.trim_blocks = True
    app.jinja_env.lstrip_blocks = True
    app.jinja_env.rstrip_blocks = True


def register_extensions(app: Flask):
    bootstrap.init_app(app)
    db.init_app(app)
    migrate.init_app(app, db)
    login_manager.login_view = 'admin.login'
    login_manager.init_app(app)


def register_blueprints(app: Flask):
    app.register_blueprint(main_bp)
    app.register_blueprint(admin_bp, url_prefix='/admin')


def register_context(app: Flask):
    @app.shell_context_processor
    def make_shell_context():
        return dict(Questionnaire=Questionnaire, Admin=Admin)
